use std::{
    net::SocketAddr,
    str::FromStr,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
};
use tweeny_rpc::{macros::rpc_service, server::Server, utils::helper_types::StaticBytes};

#[rpc_service(server, static_ref_all_under_surface_solve(req))]
trait CoolServ {
    async fn proc1(&self, bytes: StaticBytes) -> usize;
}

struct CoolServer {
    n: AtomicU64,
}

impl CoolServ for CoolServer {
    type OtherEndInfo = SocketAddr;
    fn proc1(
        &self,
        from: Self::OtherEndInfo,
        bytes: StaticBytes,
    ) -> impl Future<Output = usize> + Send {
        async move {
            let n = self.n.fetch_add(bytes.len() as u64, Ordering::Relaxed);
            n as usize + bytes.len()
        }
    }
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    env_logger::init();

    let addr = SocketAddr::from_str("127.0.0.1:22330").unwrap();
    let serv = Server::with_default_serder_tcp(
        Arc::new(CoolServServerWrapper(CoolServer {
            n: AtomicU64::new(0),
        })),
        addr,
    )
    .await?;
    println!("{:?}", tokio::spawn(serv.serve()).await);
    Ok(())
}
